package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Task;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.TaskQueryObject;
import cn.wolfcode.crm.service.ITaskService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2018/6/12.
 */

@Controller
@RequestMapping("task")
public class TaskController {

    @Autowired
    private ITaskService taskService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"task:view", "考勤页面"}, logical = Logical.OR)
    public String view() {
        return "task";
    }


    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"task:query", "考勤列表"}, logical = Logical.OR)
    public PageResult query(TaskQueryObject qo) {
        return taskService.query(qo);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"task:saveOrUpdate", "员工新增/更改"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Task task) {
        try {
            if (task.getId() != null) {
                taskService.updateByPrimaryKey(task);
            } else {
                //保存新的员工要设置默认的状态为在职
                task.setStatus(false);
                taskService.insert(task);
            }
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }
    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"task:changeState", "员工新增/更改"}, logical = Logical.OR)
    public JsonResult changeState(Task task) {
        try {
            if (task.getId() != null) {
                task.setStatus(true);
                taskService.changeState(task);
            }
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response, TaskQueryObject qo) throws IOException {
        response.setHeader("Content-disposition", "attachment;filename=task.xls");
        //创建excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("sheet01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("部门");
        row.createCell(3).setCellValue("任务");
        row.createCell(4).setCellValue("状态");
        row.createCell(5).setCellValue("内容");
        List<Task> list = taskService.selectAll();
        for (int i = 0; i < list.size(); i++) {
            Task task = list.get(i);
            row = sheet.createRow(i + 1);
            CreationHelper createHelper = workbook.getCreationHelper();
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(
                    createHelper.createDataFormat().getFormat("m/d/yy"));
            HSSFCell cellDate = row.createCell(0);
            cellDate.setCellStyle(cellStyle);
            cellDate.setCellValue(task.getDate());
            System.out.println(task.getDept().getId());
            row.createCell(1).setCellValue(task.getEmployee().getUsername());
            row.createCell(2).setCellValue(task.getDept().getName());
            row.createCell(3).setCellValue(task.getSn());
            row.createCell(4).setCellValue(task.getStatus()? "已审核":"待审核");
            row.createCell(5).setCellValue(task.getContent());
        }
        workbook.write(response.getOutputStream());
    }

}